<?php

namespace App\Http\Controllers;

use EasyWeChat\Support\Log;
use Illuminate\Http\Request;

class WechatController extends Controller
{
    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $wechat = app('wechat');
        $server = $wechat->server;
        $userService = $wechat->user;

        $server->setMessageHandler(function ($message)use($userService) {
            $res_str = '';
            switch ($message->MsgType) {
                case 'event':
                    if($message->Event=='subscribe'){
                        $res_str = '您好!关注我的个人订阅号';
                    }
                    return $res_str;
                    break;
                case 'text':
                    # 文字消息...
                    break;
                case 'image':
                    # 图片消息...
                    break;
                case 'voice':
                    # 语音消息...
                    break;
                case 'video':
                    # 视频消息...
                    break;
                case 'location':
                    # 坐标消息...
                    break;
                case 'link':
                    # 链接消息...
                    break;
                // ... 其它消息
                default:
                    # code...
                    break;
            }
            // ...
        });

        Log::info('return response.');

        return $wechat->server->serve();
    }

    public function setMenu()
    {
        $wechat = app('wechat');
        $menu = $wechat->menu;
        $buttons = [
            [
                "type" => "view",
                "name" => "博客",
                "url"  => "http://blog.anyuzhe.cc/blog"
            ],
        ];
        $menu->add($buttons);
    }
    public function test()
    {
        $wechat = app('wechat');
        $server = $wechat->server;
        $userService = $wechat->user;
//        $user = $userService->get('oVrf5wlbSNrjdv2yyAH_hG7zVcrs');
//        return $user->nickname;
        return $userService->lists($nextOpenId = null);
    }
}
